import math
from math import comb
from collections import Counter
#数学精确计算
# 牌型基础数据（根据附件统计）
TOTAL = {
    '***': 5,
    '**': 9,
    '*': 10,
    'blank': 24  # 空白牌数量
}


def exact_probability(target_counts, n):
    """精确概率计算（修正版）"""
    k1 = target_counts.get('***', 0)
    k2 = target_counts.get('**', 0)
    k3 = target_counts.get('*', 0)

    # 输入验证
    if k1 + k2 + k3 != n or n < 1 or n > 40:
        return 0.0
    if k1 > 5 or k2 > 9 or k3 > 10:
        return 0.0

    total_prob = 0.0

    # 遍历所有可能的移除组合
    for a in range(0, 5 - k1 + 1):  # 移除的***数量
        remaining_3 = 5 - a
        if remaining_3 < k1: continue

        for b in range(0, 9 - k2 + 1):  # 移除的**数量
            remaining_2 = 9 - b
            if remaining_2 < k2: continue

            for c in range(0, 10 - k3 + 1):  # 移除的*数量
                remaining_1 = 10 - c
                if remaining_1 < k3: continue

                # 计算需要移除的空白牌数量
                m = a + b + c
                if m > 8: continue
                d = 8 - m  # 移除的空白牌数量
                if d < 0 or d > 24: continue

                # 剩余有效牌总数检查
                remaining_effective = remaining_3 + remaining_2 + remaining_1
                if remaining_effective < n: continue

                # 计算组合概率
                # 步骤1: 移除8张牌的概率
                remove_prob = (
                                      comb(5, a) * comb(9, b) * comb(10, c) * comb(24, d)
                              ) / comb(48, 8)

                # 步骤2: 从剩余有效牌中抽取目标牌型的概率
                if remaining_effective == 0:
                    draw_prob = 0.0
                else:
                    draw_prob = (
                                        comb(remaining_3, k1) *
                                        comb(remaining_2, k2) *
                                        comb(remaining_1, k3)
                                ) / comb(remaining_effective, n)

                total_prob += remove_prob * draw_prob

    return total_prob


def main():
    # 示例测试：输入4张牌（1***,1**,2*）
    user_input = ['*',"*","*","*","*"]
    target_counts = Counter(user_input)
    n = len(user_input)

    # 精确计算
    exact_prob = exact_probability(target_counts, n)

    print(f"精确概率: {exact_prob:.10f}")


if __name__ == "__main__":
    main()


